package ex2.trans;

import ex2.Employee;
import ex2.PaymentClassification;
import ex2.PayrollDatabase;
import ex2.Transaction;
import ex2.classification.HourlyClassification;
import ex2.exception.NoSuchEmployeeException;
import ex2.exception.NotHourlyClassificationException;

public class TimeCardTransaction implements Transaction {

    private int empId;
    private String data;
    private double hours;

    public TimeCardTransaction(int empId, String data, double hours) {
        this.empId = empId;
        this.data = data;
        this.hours = hours;

    }

    @Override
    public void execute() {
        // 根据雇员取雇员编号
        Employee e = PayrollDatabase.getEmployee(empId);
        if (e == null) {
            throw new NoSuchEmployeeException();
        }
        // 取计算工资方式
        PaymentClassification pc = e.getPaymentClassification();
        if (pc instanceof HourlyClassification) {
            HourlyClassification hc = (HourlyClassification) pc;
            // 添加时间卡
            TimeCard tc = new TimeCard(data, hours);
            hc.addTimeCard(tc);
        } else {
            throw new NotHourlyClassificationException();
        }

    }

}
